package air2.totc.crypt;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * Implémentation de l'encodeur AES
 * @author IDIRI
 *
 */
public final class AESEncoder extends SymetricalEncoder 
{
	public AESEncoder(byte cle[]) throws NoSuchAlgorithmException, NoSuchPaddingException
	{
		this.c = Cipher.getInstance(Encoder.AESString);
		// On construit la clé..
		this.sKey = new SecretKeySpec(Encoder.padKeyToLength(cle, 256), Encoder.AESString);
	}
	

	public byte[] encrypt(byte[] bOutData) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
	{
		this.c.init(Cipher.ENCRYPT_MODE, this.sKey);
		return c.doFinal(bOutData);
	}


	public byte[] decrypt(byte[] bInData) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
	{
		this.c.init(Cipher.DECRYPT_MODE, this.sKey);
		return c.doFinal(bInData);
	}

}
